import {
  Controller,
  Post,
  Get,
  Put,
  Body,
  Param,
  Query,
  UseGuards,
  Request,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { MyMusicService } from './my-music.service';
import {
  UserSongDto,
  UserSongResponseDto,
  UserMusicStatsDto,
} from './my-music.dto';

@Controller('my-music')
export class MyMusicController {
  constructor(private readonly myMusicService: MyMusicService) {}

  // 添加或更新歌曲收藏状态
  @UseGuards(AuthGuard('jwt'))
  @Post('songs')
  async upsertUserSong(
    @Request() req,
    @Body() userSongDto: UserSongDto,
  ): Promise<UserSongResponseDto> {
    const userId = req.user.id;
    return this.myMusicService.upsertUserSong(userId, userSongDto);
  }

  // 记录歌曲播放
  @UseGuards(AuthGuard('jwt'))
  @Post('songs/:songId/play')
  async recordPlay(
    @Request() req,
    @Param('songId') songId: number,
  ): Promise<UserSongResponseDto> {
    const userId = req.user.id;
    return this.myMusicService.recordPlay(userId, songId);
  }

  // 获取用户收藏的歌曲
  @UseGuards(AuthGuard('jwt'))
  @Get('favorites')
  async getUserFavorites(@Request() req): Promise<UserSongResponseDto[]> {
    const userId = req.user.id;
    return this.myMusicService.getUserFavorites(userId);
  }

  // 获取用户的播放历史
  @UseGuards(AuthGuard('jwt'))
  @Get('play-history')
  async getUserPlayHistory(@Request() req): Promise<UserSongResponseDto[]> {
    const userId = req.user.id;
    return this.myMusicService.getUserPlayHistory(userId, 5);
  }

  // 获取用户的音乐统计信息
  @UseGuards(AuthGuard('jwt'))
  @Get('stats')
  async getUserMusicStats(@Request() req): Promise<UserMusicStatsDto> {
    const userId = req.user.id;
    return this.myMusicService.getUserMusicStats(userId);
  }

  // 切换歌曲收藏状态
  @UseGuards(AuthGuard('jwt'))
  @Put('songs/:songId/favorite')
  async toggleFavorite(
    @Request() req,
    @Param('songId') songId: number,
    @Body('isFavorite') isFavorite: boolean,
  ): Promise<UserSongResponseDto> {
    const userId = req.user.id;
    return this.myMusicService.upsertUserSong(userId, {
      songId,
      isFavorite,
    });
  }
}
